package com.jwt.server.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.jwt.server.domain.UserInfo;
import com.jwt.server.exception.UsernameIsExitedException;
import com.jwt.server.service.UserService;
import com.jwt.server.util.IdGenerator;

/**
 * @author zhaoxinguo on 2018/06/05.
 */
@RestController
@RequestMapping("/users")
public class RegisterController extends BaseController {

	  @Autowired
	  protected UserService userService;
	  
    /**
     * 注册用户 默认开启白名单
     * @param user
     */
    @PostMapping("/signup")
    public UserInfo signup(@RequestBody UserInfo user) {
        UserInfo bizUser = userService.findByUsername(user.getUsername());
        if(null != bizUser){
            throw new UsernameIsExitedException("用户已经存在");
        }
        /*user.setPassword(DigestUtils.md5DigestAsHex((user.getPassword()).getBytes()));*/
        user.setPassword(bCryptPasswordEncoder.encode(user.getPassword()));
        IdGenerator generator= IdGenerator.USER_TRANSID;
		user.setId(generator.nextId());
        return userService.save(user);
    }

}
